package com.woniu.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.woniu.entity.CreRefund;
import com.woniu.service.CreditRefundService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: Canaan
 * @description 订单控制层
 * @date: 2022/9/24 11:56
 */
@Component("refund")
@RestController
@RequestMapping("/credit/refund")
public class CreditRefundController extends BaseController {

    @Autowired
    CreditRefundService creditRefundService;


    @GetMapping("/findAll")
    @ApiOperation("查找所有订单")
    public TableDataInfo findAllRefund(CreRefund creRefund) {
        startPage();
        List<CreRefund> list = creditRefundService.findAllRefund(creRefund);
        return getDataTable(list);
    }

    @GetMapping("/checkEmailByUserId/{userId}")
    @ApiOperation("根据用户ID查邮箱")
    public String checkEmailByUserId(@PathVariable(value = "userId") int userId) {
        return creditRefundService.checkEmailByUserId(userId);
    }

    @PostMapping("/sendEmail/")
    @ApiOperation("发送邮件")
    public AjaxResult sendEmail(@RequestBody CreRefund creRefund) {
        boolean flag = creditRefundService.sendEmail(creRefund);
        String msg = "发送失败！";
        if (flag) {
            msg = "发送成功！";
        }
        return flag ? success(msg) : error(msg);
    }

    /**
     * 单次还款
     *
     * @param userid
     * @param orderid
     * @return
     */
    @ApiOperation("单次还款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userid", value = "用户id", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "orderid", value = "订单id", dataType = "int", dataTypeClass = Integer.class)
    })
    @GetMapping("/affirmPay/{userid}/{orderid}")
    public AjaxResult affirmPay(@PathVariable int userid, @PathVariable int orderid) {
        return creditRefundService.affirmPayById(userid, orderid);
    }

    /**
     * 一次性还款
     *
     * @param orderid
     * @return
     */
    @ApiOperation("单次还款")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderid", value = "订单id", dataType = "int", dataTypeClass = Integer.class)
    })
    @GetMapping("/disAffirmPay/{orderid}")
    public AjaxResult disAffirmPay(@PathVariable int orderid) {
        return creditRefundService.disAffirmPay(orderid);
    }

    @PostMapping("/export")
    public void export(HttpServletResponse response, CreRefund creRefund) {
        List<CreRefund> list = creditRefundService.findAllRefund(creRefund);
        ExcelUtil<CreRefund> util = new ExcelUtil<CreRefund>(CreRefund.class);
        util.exportExcel(response, list, "还款数据");
    }


    /**
     * 定时任务，判断是否逾期
     */
    @ApiOperation("判断合同是否逾期")
    public void refundStatus() {
        int i = creditRefundService.isOutTime();
        if (i > 0) {
            int r = creditRefundService.updateRefStatus();
            int o = creditRefundService.updateOrdStatusOut();
            if (r > 0 && o > 0) {
                System.out.println("已有" + r + "条合同逾期");
                return;
            }
        }
        System.out.println("未有逾期合同");
    }

    /**
     * 定时任务，判断是否还款
     */
    @ApiOperation("判断合同是否未还款")
    public void refundFinStatus() {
        int i = creditRefundService.isFinish();
        if (i > 0) {
            int r = creditRefundService.updateRefFinStatus();
            int o = creditRefundService.updateOrdStatusFin();
            if (r > 0 && o > 0) {
                System.out.println("已有" + r + "条合同未还款");
                return;
            }
        }
        System.out.println("未有未还款合同");
    }


}
